import Actor from "./Actor";
import Team from "./Team";

export default class BattleModel{
    teams:Team[]=[]
    aniList=[]
    constructor(){
        this.teams.push(new Team())
        this.teams[0].id=0
        this.teams.push(new Team())
        this.teams[1].id=1
        this.teams.forEach(team=>{
            team.randomActors()
        })
    }

    battle(){
        //战斗开始
        console.log("战斗开始")
        this.printInfo()
        while(!this.checkFinish()){
            this.arrangePos()
            this.printInfo()
            this.attack()
            //this.checkDie()
        }
        console.log("结束")
        this.printInfo()
    }
    private arrangePos(){
        console.log('整理位置')
        let changeArr=[]
        this.teams.forEach(team=>{
            let map=team.arrangePos()
            for(let entity of map){
                changeArr.push({
                    teamId:team.id,
                    gId:entity[0],
                    order:entity[1]
                })
            }
        })
        if(changeArr.length>0){
            this.aniList.push(
                {type:'arrangePos',changeArr}
            )
        }
    }

    private printInfo(){
        console.log("=====队伍信息====")
        this.teams.forEach(team=>{
            team.printInfo()
        })
    }
    //攻击
    private attack(){
        var actor0=this.teams[0].getFowardActor()
        var actor1=this.teams[1].getFowardActor()
        console.log(actor0,'对战',actor1)
        actor0.hp-=actor1.atk
        actor1.hp-=actor0.atk
        this.printInfo()

        //检查死亡
        let kickOut0=false
        if(actor0.hp<0){
            this.teams[actor0.teamId].removeActor(actor0)
            kickOut0=true
        }
        let kickOut1=false
        if(actor1.hp<0){
            this.teams[actor1.teamId].removeActor(actor1)
            kickOut1=true
        }

        this.aniList.push({
            type:'attack',
            infos:[
                {gId:actor0.gId,loseHp:actor1.atk,hp:actor0.hp,kickOut:kickOut0},
                {gId:actor1.gId,loseHp:actor0.atk,hp:actor1.hp,kickOut:kickOut1}
            ]

        })
    }

    // private checkDie(){
    //     let dieArr:Actor[]=[]
    //     this.teams.forEach(team=>{
    //         team.actors.forEach(actor=>{
    //             if(actor && actor.hp<=0){
    //                 dieArr.push(actor)
    //             }
    //         })
    //     })
    //     dieArr.forEach(actor=>{
    //         let team=this.teams[actor.teamId]
    //         team.removeActor(actor)
    //     })
    //     if(dieArr.length>0){
    //         this.aniList.push({
    //             type:'die',
    //             actors:dieArr.map(actor=>{
    //                 return actor.gId
    //             })
    //         })
    //     }
    // }    

    private checkFinish(){
        return this.teams[0].isEmpty() || this.teams[1].isEmpty()
    }
    
}